﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace V5_WebControls
{
    [DefaultProperty("Value")]
    [ToolboxData("<{0}:DateEditBox runat=server></{0}:DateEditBox>")]
    public class DateEditBox : CompositeControl
    {
        private TextBox txtMonth = new TextBox();
        private TextBox txtDay = new TextBox();
        private TextBox txtYear = new TextBox();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            txtMonth.ID = this.ID + "_month";
            txtMonth.MaxLength = 2;
            txtMonth.Width = this.Width;

            txtDay.ID = this.ID + "_day";
            txtDay.MaxLength = 2;
            txtDay.Width = this.Width;

            txtYear.ID = this.ID + "_year";
            txtYear.MaxLength = 4;
            txtYear.Width = this.Width;
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public DateTime Value
        {
            get
            {
                EnsureChildControls();
                if (txtMonth.Text == "" || txtDay.Text == "" ||
                    txtYear.Text == "")
                    return DateTime.MinValue;
                else
                    return new DateTime(Convert.ToInt32(txtYear.Text),
                       Convert.ToInt32(txtMonth.Text),
                       Convert.ToInt32(txtDay.Text));
            }

            set
            {
                EnsureChildControls();
                txtMonth.Text = value.Month.ToString();
                txtDay.Text = value.Day.ToString();
                txtYear.Text = value.Year.ToString();
            }
        }

        protected override void CreateChildControls()
        {
            this.Controls.Add(txtMonth);
            this.Controls.Add(txtDay);
            this.Controls.Add(txtYear);
            base.CreateChildControls();
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            txtMonth.RenderControl(writer);
            writer.RenderEndTag();    // td
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("/");
            writer.RenderEndTag();    // td
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            txtDay.RenderControl(writer);
            writer.RenderEndTag();    // td
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.Write("/");
            writer.RenderEndTag();    // td
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            txtYear.RenderControl(writer);
            writer.RenderEndTag();    // td
            writer.RenderEndTag();    // tr
            writer.RenderEndTag();    // table
        }
    }
}
